package com.example.webpractice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.webpractice.annotation.IgnoreAuth;
import com.example.webpractice.dao.XueshengDao;
import com.example.webpractice.dto.CalcDTO;
import com.example.webpractice.dto.UserTestDTO;
import com.example.webpractice.po.XueshengPO;
import com.example.webpractice.service.CalcService;
import com.example.webpractice.service.XueshengService;
import com.example.webpractice.utils.Result;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.UserTestVO;
import com.example.webpractice.vo.XueshengVO;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试
 * 后端接口
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private CalcService calcService;

    @Autowired
    private XueshengService xueshengService;
    @Autowired
    private XueshengDao dao;

    @IgnoreAuth
    @RequestMapping("/cal1")
    public int calGet1(@RequestParam(name="a") int a,
                       @RequestParam(name="b") int b,
                       @RequestParam(name="op",required =false) String op) {
        if (StringUtils.isBlank(op)) {
            op = "+";
        }
        int c;
        switch (op){
            case "+":
                c=a+b;break;
            case "-":
                c=a-b;break;
            case "*":
                c=a*b;break;
            case "/":
                if (b==0){
                    c=0;break;
                }
                c=a/b;break;
            default:
                c=0;
        }
        return c;
    }
    @IgnoreAuth
    @RequestMapping("/cal2")
    public Result calGet2(@RequestParam(name="a") int a,
                          @RequestParam(name="b") int b,
                          @RequestParam(name="op",required =false) String op) {
        Result r =  new Result();
        if (StringUtils.isBlank(op)) {
            op = "+";
        }
        if(!(op.equals("+")||op.equals("-")||op.equals("*")||op.equals("/"))){
            r.setCode(-1);
            r.setErrMsg("只支持加减乘除");
            return r;
        }
        if (op.equals("/") && b==0){
            r.setCode(-2);
            r.setErrMsg("除数不能为0");
            return r;
        }
        int c;
        switch (op){
            case "+":
                c=a+b;
                break;
            case "-":
                c=a-b;
                break;
            case "*":
                c=a*b;
                break;
            case "/":
                if (b==0){
                    c=0;
                    break;
                }
                c=a/b;
                break;
            default:
                c=0;
        }
        r.setCode(0);
        r.setResult(c);
        return r;
    }
    @IgnoreAuth
    @RequestMapping("/cal3")
    public Result calGet3(@RequestParam(name="a") int a,
                          @RequestParam(name="b") int b,
                          @RequestParam(name="op",required =false) String op) {
        Result r = new Result();
        if(!(op.equals("+")||op.equals("-")||op.equals("*")||op.equals("/"))){
            r.setCode(-1);
            r.setErrMsg("只支持加减乘除");
            return r;
        }
        if (op.equals("/") && b==0){
            r.setCode(-1);
            r.setErrMsg("除数不能为0");
            return r;
        }
        int c =  calcService.Calc(a,b,op);
        r.setCode(0);
        r.setResult(c);
        return r;
    }

    @IgnoreAuth
    @RequestMapping("/cal4")
    public Result calPost4(@RequestBody CalcDTO dto) {
        if (dto.getA()==null||dto.getB()==null){
            return new Result(-3,"a,b均不能是空",0);
        }
        String op = dto.getOp();
        if(op == null){
            op = "+";
        }
        Result r = new Result();
        int c =  calcService.Calc(dto.getA(),dto.getB(),op);
        r.setCode(0);
        r.setResult(c);
        return r;
    }

    @IgnoreAuth
    @RequestMapping("/xuesheng/list")
    public ServerResponseEntity xueshengGet1(@RequestParam(name="xuehao", required = false) String xuehao) {
        QueryWrapper<XueshengPO> query = new QueryWrapper<>();
        if (!StringUtils.isBlank(xuehao)) {
            query.like("xuehao",xuehao);
        }
        List<XueshengPO>  listpo =  dao.selectList(query);
        List<XueshengVO>  listvo = new ArrayList<>();
        for (XueshengPO po:listpo){
            XueshengVO xueshengVO = new XueshengVO();
            xueshengVO.setId(po.getId());
            xueshengVO.setShoujihaoma(po.getShoujihaoma());
            xueshengVO.setXiangpian(po.getXiangpian());
            xueshengVO.setXingbie(po.getXingbie());
            xueshengVO.setXueshengxingming(po.getXueshengxingming());
            xueshengVO.setYouxiang(po.getYouxiang());
            listvo.add(xueshengVO);
        }
        return ServerResponseEntity.success(listvo);
    }

    @IgnoreAuth
    @RequestMapping("/xuesheng")
    public ServerResponseEntity xueshengGet2(@RequestParam(name="id") long id) {
        XueshengPO po =  dao.selectById(id);
        XueshengVO vo = new XueshengVO();
        BeanUtils.copyProperties(po,vo);
        return ServerResponseEntity.success(vo);
    }

    @IgnoreAuth
    @RequestMapping("/xuesheng/{id}")
    public ServerResponseEntity xueshengGet3(@PathVariable(name="id") long id) {
        XueshengVO vo = xueshengService.selectById(id);
        return ServerResponseEntity.success(vo);
    }

    /**
     * @param name 用户名
     * @param pwd  密码
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/addUser1")
    public String addUser1(@RequestParam(name = "name") String name, @RequestParam(name = "pwd") String pwd) {
        log.info("name:" + name + ",pwd:" + pwd);
        return "name:" + name + " 1,pwd:" + pwd + " 1";
    }

    @IgnoreAuth
    @RequestMapping(path = "/addUser2/{name}/{pwd}")
    public String addUser2(@PathVariable(value = "name") String name, @PathVariable(value = "pwd") String pwd) {
        return "name:" + name + " 2,pwd:" + pwd + " 2";
    }

    @IgnoreAuth
    @RequestMapping("/addUser3")
    public UserTestVO addUser3(@RequestBody UserTestDTO user) {
        UserTestVO reponseUser = new UserTestVO();
        reponseUser.setId(user.getId() + 2);
        reponseUser.setName(user.getName() + " 2");
        return reponseUser;
    }

}
